//
//  MainViewController.m
//  小胖兔旅行指南
//
//  Created by 小胖兔 on 15/4/10.
//  Copyright (c) 2015年 小胖兔. All rights reserved.
//

#import "MainViewController.h"
#import "MyConnection.h"
#import "MyTableViewCell.h"
#import "DetailSpecialTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"
#import "DetailSpecialViewController.h"
#import "SmallTableViewCell.h"

@interface MainViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *tableArray;

@end

@implementation MainViewController

-(void)dealloc
{
    [_tableArray release];
    [_table release];
    
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

#pragma mark -
#pragma mark 创建TableView
- (void)createView
{
    self.table = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain] autorelease];
    [self.view addSubview:_table];
    _table.dataSource = self;
    _table.delegate = self;
    [_table registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_table registerClass:[DetailSpecialTableViewCell class] forCellReuseIdentifier:@"Scell"];
    [_table registerClass:[SmallTableViewCell class] forCellReuseIdentifier:@"smallCell"];
    [_table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createView];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"0" forKey:@"qid"];
    [dic setObject:@"1" forKey:@"page"];
    
    
    __block MainViewController *my = self;
    [MyConnection connectWithUrl:Login parmaters:dic finishBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [dic objectForKey:@"recomms"];
        my.tableArray = array;
        [my.table reloadData];
    }];
    
    
    self.title = @"精选";
}

#pragma mark -
#pragma mark UItableViewDataSource协议的方法的重写

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 2) {
        return 100;
    }
    return 200;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 1) {
        DetailSpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Scell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSMutableDictionary *adic = [self.tableArray objectAtIndex:indexPath.row];
        NSMutableArray *aArray = [adic objectForKey:@"collections"];
        NSMutableDictionary *dic0 = [aArray objectAtIndex:0];
        NSMutableArray *array0 = [dic0 objectForKey:@"bg_pic"];
        NSString *str0 = [array0 objectAtIndex:0];
        NSURL *url0 = [NSURL URLWithString:str0];
        NSString *title0 = [dic0 objectForKey:@"title"];
        [cell.aImage sd_setImageWithURL:url0];
        [cell.aLable setText:title0];
        NSNumber *aNumber = [dic0 objectForKey:@"id"];
        NSInteger aTag = [aNumber integerValue];
        [cell.aButton setTag:aTag];
        [cell.aButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        NSMutableDictionary *dic1 = [aArray objectAtIndex:1];
        NSMutableArray *array1 = [dic1 objectForKey:@"bg_pic"];
        NSString *str1 = [array1 objectAtIndex:0];
        NSURL *url1 = [NSURL URLWithString:str1];
        [cell.bImage sd_setImageWithURL:url1];
        NSString *title1 = [dic1 objectForKey:@"title"];
        [cell.bLable setText:title1];
        NSNumber *bNumber = [dic1 objectForKey:@"id"];
        NSInteger bTag = [bNumber integerValue];
        [cell.bButton setTag:bTag];
        [cell.bButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        NSMutableDictionary *dic2 = [aArray objectAtIndex:2];
        NSMutableArray *array2 = [dic2 objectForKey:@"bg_pic"];
        NSString *str2 = [array2 objectAtIndex:0];
        NSURL *url2 = [NSURL URLWithString:str2];
        [cell.cImage sd_setImageWithURL:url2];
        NSString *title2 = [dic2 objectForKey:@"title"];
        [cell.cLable setText:title2];
        NSNumber *cNumber = [dic2 objectForKey:@"id"];
        NSInteger cTag = [cNumber integerValue];
        [cell.cButton setTag:cTag];
        [cell.cButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    } else  if (indexPath.row == 2){
        
        SmallTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"smallCell"];
        NSMutableDictionary *bdic = [self.tableArray objectAtIndex:indexPath.row];
        NSMutableArray *barray = [bdic objectForKey:@"collections"];
        NSMutableDictionary *bdic0 = [barray objectAtIndex:0];
        NSMutableArray *barray0 = [bdic0 objectForKey:@"bg_pic"];
        NSString *bstr0 = [barray0 objectAtIndex:0];
        NSURL *burl0 = [NSURL URLWithString:bstr0];
        [cell.dImage sd_setImageWithURL:burl0];
        NSString *btitle0 = [bdic0 objectForKey:@"title"];
        [cell.dLable setText:btitle0];
        NSNumber *dNumber = [bdic0 objectForKey:@"id"];
        NSInteger dTag = [dNumber integerValue];
        [cell.dButton setTag:dTag];
        [cell.dButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        NSMutableDictionary *bdic1 = [barray objectAtIndex:1];
        NSMutableArray *barray1 = [bdic1 objectForKey:@"bg_pic"];
        NSString *bstr1 = [barray1 objectAtIndex:0];
        NSURL *burl1 = [NSURL URLWithString:bstr1];
        [cell.eImage sd_setImageWithURL:burl1];
        NSString *btitle1 = [bdic1 objectForKey:@"title"];
        [cell.eLable setText:btitle1];
        NSNumber *eNumber = [bdic1 objectForKey:@"id"];
        NSInteger eTag = [eNumber integerValue];
        [cell.eButton setTag:eTag];
        [cell.eButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    } else {
        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic = [self.tableArray objectAtIndex:indexPath.row];
        NSMutableArray *array = [dic objectForKey:@"bg_pic"];
        NSString *str = [array objectAtIndex:0];
        
        NSURL *url = [NSURL URLWithString:str];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"加载图片" ofType:@"jpg"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        UIImage *image = [UIImage imageWithData:data];
        [cell.image sd_setImageWithURL:url placeholderImage:image];
        [cell.title setText:[dic objectForKey:@"title"]];
        
        NSString *address = [NSString stringWithFormat:@"🏁%@", [dic objectForKey:@"destination"]];
        [cell.address setText:address];
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.table deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 1 || indexPath.row == 2) {
        return;
    } else {
        DetailViewController *det = [[[DetailViewController alloc] init] autorelease];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic = [self.tableArray objectAtIndex:indexPath.row];
        NSNumber *num = [dic objectForKey:@"id"];
        NSInteger tag = [num integerValue];
        det.tag = tag;
        [self.navigationController pushViewController:det animated:YES];
    }
}


- (void)buttonClick:(id)sender
{
    UIButton *aButton = (UIButton *)sender;
    DetailSpecialViewController *ds = [[[DetailSpecialViewController alloc] init] autorelease];
    ds.tag = aButton.tag;
    [self.navigationController pushViewController:ds animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
